define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/index' + location.search,
                    add_url: 'order/add',
                    edit_url: 'order/edit',
                    del_url: 'order/del',
                    multi_url: 'order/multi',
                    import_url: 'order/import',
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                searchFormVisible: true,// 默认显示搜索条件
                search: false,// 搜索框
                // commonSearch: false,// 搜索按钮
                showToggle: false,// 切换视图
                showColumns: false,// 显示列
                showExport: false,// 导出按钮
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'user.nickname', title: __('下单用户'), operate: false},
                        {field: 'steward.realname', title: __('接单管家'), operate: false},
                        {field: 'order_no', title: __('Order_no'), operate: 'LIKE'},
                        {field: 'package.title', title: __('服务套餐'), operate: false, table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'student_name', title: __('Student_name'), operate: 'LIKE', visible: false, table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'school_addr', title: __('School_addr'), operate: 'LIKE', visible: false, table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'home_addr', title: __('Home_addr'), operate: 'LIKE', visible: false, table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'student', title: __('学生档案'), operate: false, table: table, class: 'autocontent', width:120, align: 'left',
                            formatter:function(value,row,index){
                                var html='';
                                html += "<span style='display: block;overflow: hidden;text-overflow:ellipsis;white-space: nowrap;' title='" + row.student_name + "'>学生姓名：" + row.student_name + "</span>";
                                html += "<span style='display: block;overflow: hidden;text-overflow:ellipsis;white-space: nowrap;' title='" + row.school_addr + "'>学校地址：" + row.school_addr + "</span>";
                                html += "<span style='display: block;overflow: hidden;text-overflow:ellipsis;white-space: nowrap;' title='" + row.home_addr + "'>家庭地址：" + row.home_addr + "</span>";
                                return html;
                            },
                            // 固定列最大宽度，超出隐藏
                            cellStyle : function(value, row, index, field){
                                return {
                                    css: {
                                        "white-space": "nowrap",
                                        "text-overflow": "ellipsis",
                                        "overflow": "hidden",
                                        "max-width":"200px"
                                    }
                                };
                            }
                        },
                        {field: 'pay_price', title: __('支付金额'), operate: false},
                        {field: 'pay_type', title: __('支付方式'), searchList: {"0":__('未支付'),"1":__('微信'),"2":__('支付宝')}, formatter: Table.api.formatter.label},
                        {field: 'state', title: __('状态'), searchList: {"-1":__('待付款'),"0":__('待接单'),"1":__('服务中'),"2":__('已完成'),"7":__('已取消')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('按钮组'),
                            table: table,
                            events: Table.api.events.operate,
                            operate: false,
                            buttons: [
                                {
                                    name: 'details',
                                    title: __('详情'),
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    icon: 'fa fa-eye',
                                    url: 'order/details'
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        details: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
